package com.whmskj.xjlhsc.mobie.ProductionManagement.service.imp;

import com.whmskj.xjlhsc.mobie.ProductionManagement.mapper.YieldReportsMapper;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.dto.GetYieldReportsDTO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.dto.MonthYieldReportsDTO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.vo.MonthYieldReportsVO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.vo.YieldReportsResultVO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.pojo.vo.YieldVO;
import com.whmskj.xjlhsc.mobie.ProductionManagement.service.IYieldReportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-08-29 10:38
 */
@Slf4j
@Service
public class YieldReportsServiceImpl implements IYieldReportsService {

    @Autowired
    private YieldReportsMapper yieldReportsMapper;

    @Override
    public YieldVO getYieldReports(GetYieldReportsDTO getYieldReportsDTO) {
        String process = getYieldReportsDTO.getProcess();
        String dayOrMonth = getYieldReportsDTO.getDayOrMonth();
        if (dayOrMonth.equals("day")) {
            YieldVO yieldVO = new YieldVO();
            LocalDate dayTime = LocalDate.parse(getYieldReportsDTO.getDayTime());
            List<YieldReportsResultVO> yieldReportsByDay = yieldReportsMapper.getYieldReportsByDay(process, dayTime.toString());
            for (int i = 0; i < yieldReportsByDay.size(); i++) {
                yieldReportsByDay.get(i).setIndex(i);
            }
            log.info("yieldReportsByDay:{}", yieldReportsByDay);
            for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByDay) {
                yieldReportsResultVO.setDayYieldIncrease(yieldReportsResultVO.getDayActualYield() - yieldReportsResultVO.getDayPlanYield());
            }
            Double totalDayPlanYield = 0.0;
            Double totalDayActualYield = 0.0;
            for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByDay) {
                totalDayPlanYield += yieldReportsResultVO.getDayPlanYield();
                totalDayActualYield += yieldReportsResultVO.getDayActualYield();
            }
            yieldVO.setTotalDayPlanYield(totalDayPlanYield);
            yieldVO.setTotalDayActualYield(totalDayActualYield);
            Integer totalOpeningNumber = 0;
            for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByDay) {
                totalOpeningNumber += yieldReportsResultVO.getOpeningNumber();
            }
            yieldVO.setTotalOpeningNumber(totalOpeningNumber);
            yieldVO.setYieldReportsResultVOList(yieldReportsByDay);
            Double totalDayYieldIncrease = 0.0;
            for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByDay) {
                totalDayYieldIncrease += yieldReportsResultVO.getDayYieldIncrease();
            }
            yieldVO.setTotalDayYieldIncrease(totalDayYieldIncrease);
            //yieldVO的所有小数保留两位
            yieldVO.setTotalDayYieldIncrease(
                    new java.math.BigDecimal(totalDayYieldIncrease)
                            .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
            yieldVO.setTotalDayPlanYield(
                    new java.math.BigDecimal(totalDayPlanYield)
                            .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
            yieldVO.setTotalDayActualYield(
                    new java.math.BigDecimal(totalDayActualYield)
                            .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
            for (YieldReportsResultVO yieldReportsResultVO : yieldVO.getYieldReportsResultVOList()) {
                //yieldReportsResultVO的所有小数保留两位
                yieldReportsResultVO.setDayPlanYield(
                        java.math.BigDecimal.valueOf(yieldReportsResultVO.getDayPlanYield())
                                .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
                yieldReportsResultVO.setDayActualYield(
                        java.math.BigDecimal.valueOf(yieldReportsResultVO.getDayActualYield())
                                .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
                yieldReportsResultVO.setDayYieldIncrease(
                        java.math.BigDecimal.valueOf(yieldReportsResultVO.getDayYieldIncrease())
                                .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
            }
            //络筒效率
            if (process.equals("络筒")) {
                Double efficiency = 0.0;
                //效率
                for (YieldReportsResultVO yieldReportsResultVO : yieldVO.getYieldReportsResultVOList()) {
                    if(yieldReportsResultVO.getDayPlanYield() != 0){
                        //保留两位小数
                        yieldReportsResultVO.setEfficiency(
                                java.math.BigDecimal.valueOf(yieldReportsResultVO.getDayActualYield() / yieldReportsResultVO.getDayPlanYield())
                                        .setScale(2, java.math.RoundingMode.HALF_UP).doubleValue());
                    }
                }
                //总效率
                if(yieldVO.getTotalDayPlanYield() != 0){
                    //保留两位小数
                    yieldVO.setEfficiency(
                            java.math.BigDecimal.valueOf(yieldVO.getTotalDayActualYield() / yieldVO.getTotalDayPlanYield())
                                    .setScale(2, java.math.RoundingMode.HALF_UP).doubleValue());
                }else {
                    yieldVO.setEfficiency(0.0);
                }
            }
            return yieldVO;
        }
        //------------------------------------月统计
        String monthStart = getYieldReportsDTO.getMonthTime().substring(8, 18);
        String monthEnd = getYieldReportsDTO.getMonthTime().substring(20, 31);
        YieldVO yieldVO = new YieldVO();
        List<YieldReportsResultVO> yieldReportsByMonth = yieldReportsMapper.getYieldReportsByMonth(process, monthStart,monthEnd);
        for (int i = 0; i < yieldReportsByMonth.size(); i++) {
            yieldReportsByMonth.get(i).setIndex(i);
        }
            log.info("yieldReportsByMonth:{}", yieldReportsByMonth);
        for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByMonth) {
            yieldReportsResultVO.setMonthYieldIncrease(yieldReportsResultVO.getMonthActualYield() - yieldReportsResultVO.getMonthPlanYield());
        }
        Double totalMonthPlanYield = 0.0;
        Double totalMonthActualYield = 0.0;
        for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByMonth) {
            totalMonthPlanYield += yieldReportsResultVO.getMonthPlanYield();
            totalMonthActualYield += yieldReportsResultVO.getMonthActualYield();
        }
        yieldVO.setTotalMonthPlanYield(totalMonthPlanYield);
        yieldVO.setTotalMonthActualYield(totalMonthActualYield);
        Integer totalMonthOpeningNumber = 0;
        for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByMonth) {
            totalMonthOpeningNumber += yieldReportsResultVO.getOpeningNumber();
        }
        yieldVO.setTotalOpeningNumber(totalMonthOpeningNumber);
        Double totalMonthYieldIncrease = 0.0;
        for (YieldReportsResultVO yieldReportsResultVO : yieldReportsByMonth) {
            totalMonthYieldIncrease += yieldReportsResultVO.getMonthYieldIncrease();
        }
        yieldVO.setTotalMonthYieldIncrease(totalMonthYieldIncrease);
        yieldVO.setYieldReportsResultVOList(yieldReportsByMonth);
        //yieldVO的所有小数保留两位
        yieldVO.setTotalMonthYieldIncrease(
                new java.math.BigDecimal(totalMonthYieldIncrease)
                        .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
        yieldVO.setTotalMonthPlanYield(
                new java.math.BigDecimal(totalMonthPlanYield)
                        .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
        yieldVO.setTotalMonthActualYield(
                new java.math.BigDecimal(totalMonthActualYield)
                        .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
        for (YieldReportsResultVO yieldReportsResultVO : yieldVO.getYieldReportsResultVOList()) {
            //yieldReportsResultVO的所有小数保留两位
            yieldReportsResultVO.setMonthPlanYield(
                    java.math.BigDecimal.valueOf(yieldReportsResultVO.getMonthPlanYield())
                            .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
            yieldReportsResultVO.setMonthActualYield(
                    java.math.BigDecimal.valueOf(yieldReportsResultVO.getMonthActualYield())
                            .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
            yieldReportsResultVO.setMonthYieldIncrease(
                    java.math.BigDecimal.valueOf(yieldReportsResultVO.getMonthYieldIncrease())
                            .setScale(3, java.math.RoundingMode.HALF_UP).doubleValue());
        }
        return yieldVO;
    }

    @Override
    public List<MonthYieldReportsVO> getMonthYieldReports() {
        List<MonthYieldReportsDTO> panCunMonthYieldReports = yieldReportsMapper.getPanCunMonthYieldReports();
        List<MonthYieldReportsVO> monthYieldReportsVOList = new ArrayList<>();
        for (MonthYieldReportsDTO monthYieldReportsDTO : panCunMonthYieldReports) {
            String[] split = monthYieldReportsDTO.getMonth().split("-");
            String r = split[0] + "-" + split[1] + "（" + monthYieldReportsDTO.getOtherOrganiser() + "晚" + "~ " + monthYieldReportsDTO.getEndDate() + "早" + "）";
            //如果monthYieldReportsDTO.getOtherOrganiser()在当前时间之前，则add
            if (LocalDate.parse(monthYieldReportsDTO.getOtherOrganiser()).isBefore(LocalDate.now())) {
                monthYieldReportsVOList.add(new MonthYieldReportsVO(r));
            }
        }
        //顺序颠倒
        Collections.reverse(monthYieldReportsVOList);
        return monthYieldReportsVOList;
    }
}